use std::env;
use std::char;
use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;

fn main() {
    if env::args().len() != 2 {
       println!("Usage: {} <path to words file>", env::args().nth(0).unwrap()); 
       return;
    }

    let filename = env::args().nth(1).unwrap();
    let f = File::open(filename).ok().expect("failed open words file");
    let reader = BufReader::new(&f);
    let mut longest_ordered = String::new();

    'lines: for line in reader.lines() {
        let l = line.ok().expect("failed to read from words file");

        if longest_ordered.len() >= l.len() {
            continue;
        }

        let mut last_char: char = char::from_u32(0).unwrap();

        for c in l.chars() {
            if last_char <= c {
                last_char = c;
            } else {
                continue 'lines;
            }
        }

        longest_ordered = l;
    }

    println!("Longest ordered string: {}", longest_ordered);
}

